package com.cloud.xtools.module.service.admin.impl;

import cn.hutool.core.bean.BeanUtil;
import com.baomidou.mybatisplus.extension.service.impl.ServiceImpl;
import com.cloud.xtools.common.response.PageList;
import com.cloud.xtools.common.util.Dump;
import com.cloud.xtools.common.util.XPage;
import com.cloud.xtools.module.domain.entity.Module;
import com.cloud.xtools.module.domain.query.admin.ModuleQuery;
import com.cloud.xtools.module.domain.request.admin.ModuleCreateRequest;
import com.cloud.xtools.module.domain.request.admin.ModuleUpdateRequest;
import com.cloud.xtools.module.domain.resource.admin.ModuleResource;
import com.cloud.xtools.module.mapper.ModuleMapper;
import com.cloud.xtools.module.service.admin.ModuleService;
import org.springframework.stereotype.Service;

@Service
public class ModuleServiceImpl extends ServiceImpl<ModuleMapper, Module> implements ModuleService {
    @Override
    public PageList moduleList(ModuleQuery moduleQuery) {

        XPage.startPage(moduleQuery.getPageNum(), moduleQuery.getPageSize());
        return XPage.page(list(), ModuleResource.class);
    }

    @Override
    public ModuleResource add(ModuleCreateRequest moduleCreateRequest) {
        Module module = BeanUtil.copyProperties(moduleCreateRequest, Module.class);
        Dump.json(module);
        save(module);
        return BeanUtil.copyProperties(module, ModuleResource.class);
    }

    @Override
    public void edit(Long id, ModuleUpdateRequest moduleUpdateRequest) {
        moduleUpdateRequest.setId(id);
        updateById(BeanUtil.copyProperties(moduleUpdateRequest, Module.class));
    }

    @Override
    public ModuleResource getModuleById(Long id) {
        return BeanUtil.copyProperties(getById(id), ModuleResource.class);
    }
}
